<?php
class Admin_PresseController extends My_Controller_Admin_Action
{
	protected $presseModel = '';

	protected function _init()
	{
		$this->presseModel = new Admin_Model_Presse();
	}

	public function indexAction()
	{
		$rowPerPage = $this->_getParam('rowperpage', 20);
		$currentPage = 1;
		$page = $this->_getParam('page', 1);
		if(!empty($page)) {
			$currentPage = $page;
		}

		$rows = $this->presseModel->getListPresse();
		$paginator = Zend_Paginator::factory($rows);
		$paginator->setItemCountPerPage($rowPerPage);
		$paginator->setCurrentPageNumber($currentPage);
		
		$this->view->page = $page;
		$this->view->paginator = $paginator;
	}

	public function addPresseAction()
	{
		$id = $this->_getParam('id');
		if(!$id)
		{
			$this->view->title = 'Ajout';
		}else{
			$this->view->title = 'Editer';
			$pre = new Admin_Model_Presse();
			$row = $pre->getPresse($id);
			
			$this->view->pre = $row ;
		}
	}

	public function savePresseAction()
	{
	    $post = $this->getRequest()->getPost();
	    
	    $data = array(
            'titre'      => $post['titre'],
            'resume'     => addslashes($post['resume']),
            'contenu'    => addslashes($post['contenu']),
            'dtcreat'    => date('Y-m-d'),
            'soustitre'  => addslashes($post['soustitre'])
        );
        
	    if($post['dtpubli']) {
	        $dtpubli = explode('/', $post['dtpubli']);
            $data['dtpubli'] = date("Y-m-d", mktime(0,0,0,$dtpubli[1], $dtpubli[0], $dtpubli[2]));
        }
        
        $presseModel = new Admin_Model_Presse();
        $num_presse = $presseModel->insert($data);
        
        $upload_dir = APPLICATION_PATH . '/../public/presse/';
    	if ($_FILES['userfile']['tmp_name'] != "") {
    		$file = $_FILES['userfile'];
    		$test = preg_match('#\.(.+)$#', $file['name'], $matches);
    		$extension = $matches[1];		
    		$nom_pub = $num_presse .".".$extension;
    		//echo "fichier : " . $nom_pub."<br>";
    		if (move_uploaded_file($_FILES['userfile']['tmp_name'], $upload_dir.$nom_pub)) {
    		    $presseModel->update(array('fichier' => $nom_pub), 'p_presse=' . $num_presse);
    		}	
    	}
    	if ($_FILES['userfile0']['tmp_name'] != "") {
    		$file = $_FILES['userfile0'];
    		$test = preg_match('#\.(.+)$#', $file['name'], $matches);
    		$extension = $matches[1];		
    		$nom_pub = $num_presse."_thumb.".$extension;
    		//echo "vignette : " . $nom_pub;
    		if (move_uploaded_file($_FILES['userfile0']['tmp_name'], $upload_dir.$nom_pub)) {
    		    $presseModel->update(array('vignette' => $nom_pub), 'p_presse=' . $num_presse);
    		}	
    	}	
    	
    	$this->_redirect('admin/presse');    	
	}
	
	public function updatePresseAction()
	{
	    $post = $this->getRequest()->getPost();
	    
	    $data = array(
            'titre'      => addslashes($post['titre']),
            'resume'     => addslashes($post['resume']),
            'contenu'    => addslashes($post['contenu']),
            'dtcreat'    => date('Y-m-d'),
            'soustitre'  => addslashes($post['soustitre']),
	        'num_temp'   => md5(uniqid(rand(), true))
        );
        
	    if($post['dtpubli']) {
            $dtpubli = explode('/', $post['dtpubli']);
            $data['dtpubli'] = date("Y-m-d", mktime(0,0,0,$dtpubli[1], $dtpubli[0], $dtpubli[2]));
        }
        
        $num_presse  = $post['p_presse'];
        
        $presseModel = new Admin_Model_Presse();
        $num_presse = $presseModel->update($data, 'p_presse = ' . $num_presse);
        
        $upload_dir = APPLICATION_PATH . '/../public/presse/';
    	// Si on ne veut pas conserver le fichier, on le delete
    	if ($post[conserve] != "") {
    		if ($post[conserve] != 1) {
    			$fichier = $upload_dir . $post[conserve];
    			unlink($fichier);
    			$presseModel->update(array('fichier' => ''), 'p_presse=' . $num_presse);
    		}
    	}
    	
    	
    	// Insertion d'un nouveau fichier
    	if ($_FILES['userfile']['tmp_name'] != "") {
    		$file = $_FILES['userfile'];
    		$test = preg_match('#\.(.+)$#', $file['name'], $matches);
    		$extension = $matches[1];	
    		$nom_pub = $num_presse .".".$extension;
    		if (move_uploaded_file($_FILES['userfile']['tmp_name'], $upload_dir.$nom_pub)) {
    			if ($post[conserve] == 1) {
    			    $row = $presseModel->getFichier($num_presse);
    				$fichier = $upload_dir . $row['fichier'];
    				unlink($fichier);
    			}
    			
    			$presseModel->update(array('fichier' => $nom_pub), 'p_presse=' . $num_presse);
    		}	
    	}	
    	// Si on ne veut pas conserver la vignette, on la delete
    	if ($post[conserveVI] != "") {
    		if ($post[conserveVI] != 1) {
    			$fichier = $upload_dir . $_POST[conserveVI];
    			unlink($fichier);
    			$presseModel->update(array('vignette' => ''), 'p_presse=' . $num_presse);
    		}
    	}
    	// Insertion d'une nouvelle vignette
    	if ($_FILES['userfile0']['tmp_name'] != "") {
    		$file = $_FILES['userfile0'];
    		$test = preg_match('#\.(.+)$#', $file['name'], $matches);
    		$extension = $matches[1];	
    		$nom_pub = $_GET[num] .".".$extension;
    		if (move_uploaded_file($_FILES['userfile0']['tmp_name'], $upload_dir.$nom_pub)) {
    			if ($post[conserveVI] == 1) {
    			    $row = $presseModel->getVignette($num_presse);
    				$vignette = $upload_dir . $row['vignette'];
    				unlink($vignette);
    			}
    			
    			$presseModel->update(array('vignette' => $nom_pub), 'p_presse=' . $num_presse);
    		}	
    	}	
    
    	$this->_redirect('admin/presse');
	}
	
	public function deletePresseAction()
	{
		$id = $this->_getParam('id');
		$page = $this->_getParam('page', 1);
		$presseModel = new Admin_Model_Presse();
		$presseModel->removePresse($id);

		$param = '';
		if($page > 1) $param = 'index/page/' . $page;
		$this->_redirect('admin/presse/' . $param);
		exit;
	}
}

